<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<% String paramType = request.getParameter("paramType"); %>
<% String paramId = request.getParameter("paramId"); %>
  <head>
    <title>参数值添加页面</title>
    <script type="text/javascript">
    	var paramType = '<%=paramType %>';
    	var paramId = '<%=paramId %>';
    	
    	var _initSelectType = function() {
    		var selectTypes = $('#paramValSelect select');
    		$.each(selectTypes, function(index, selectType) {
    			if ($(selectType).attr("id") == paramType) {
    				$(selectType).show();
    			} else {
    				$(selectType).hide();
    			}
    		});
    	}
    	var _init = function() {
    		if ((paramType == "S00") ||(paramType == "S02")) {
    			$('#paramValInput').show();
    			$('#paramValSelect').hide();
    		} else {
    			$('#paramValSelect').show();
    			$('#paramValInput').hide();
    			_initSelectType();
    		}
    	}
    	
    	var _savaDefautValue = function(paramValue, paramText,paramType) {
    		var paramDiv = $('div[id="action_param_' + paramId + '"]');
			var valueArr = paramValue.split("|");
    		var valueTextArr = paramText.split("|");
    		var param_value = $(paramDiv).find("select[id='param_value']");
			var defaultValue;
			if (paramType == PACKAGE.PARAMETERTYPE.key.S02) {
				defaultValue =  $($($(paramDiv).find('tr')[4]).find('td')[1]);
			} else {
				defaultValue = $(paramDiv).find("select[id='param_def_value']");
			}
			
    		var paramValues =  $(paramDiv).find("select[id='paramValues']");
			$(param_value).html("");
			$(defaultValue).html("");
			$(paramValues).html("");
			$(paramValues).parent().show();
			for (var i = 0; i < valueArr.length; i++) {
				var value = valueArr[i];
				var valueText = valueTextArr[i];
				//多选N业务，换成checkbox
				if (paramType == PACKAGE.PARAMETERTYPE.key.S02) {
					var opt = $('<input type="checkbox">').attr('name',value);
					$(defaultValue).append(value);
					$(defaultValue).append(opt);
				} else {
					var opt = $("<option>").text(valueText).attr("value", value);
					$(defaultValue).append(opt);
				}
				
				
				var opt1 = $("<option>").text(valueText).attr("value", value);
				$(paramValues).append(opt1);
				
				var opt2 = $("<option>").text(valueText).val(value);
				$(param_value).append(opt2);
			}
    	}
    	var _ifNewValueExists = function(value, newValue) {
    		var valueArr = value.split("|");
    		var ifExists = false;
    		for (var i = 0; i < valueArr.length; i++) {
    			if (newValue == valueArr[i]) {
    				ifExists = true;
    				break;
    			}
    		}
    		return ifExists;
    	}
    	
    	var _getParamValueDesc = function(paramValue, paramType) {
			var paramValueDesc = paramValue;
			if (paramType == "S01") {//新装已有不需要
				var valueArr = paramValue.split("|");
				var tempDesc = "";
				$.each(valueArr, function(index, value) {
					if (index == 0) {
						tempDesc = ACTION.PROD_ACTION_TYPE[value];
					} else {
						tempDesc += "|" + ACTION.PROD_ACTION_TYPE[value];
					}
				});
				paramValueDesc = tempDesc;
			} else if (paramType == "S03") {//付费方式
				var valueArr = paramValue.split("|");
				var tempDesc = "";
				$.each(valueArr, function(index, value) {
					if (index == 0) {
						tempDesc = ACTION.FEE_TYPE[value];
					} else {
						tempDesc += "|" + ACTION.FEE_TYPE[value];
					}
				});
				paramValueDesc = tempDesc;
			}
			return paramValueDesc;
		}
    	var doSave = function() {
    		var paramDiv = $('div[id="action_param_' + paramId + '"]').find('table');
    		var paramValue = $(paramDiv).find('select[id="param_value"]').attr("val").trim();
    		var newParamValue = "";
    		if ((paramType == "S00") || (paramType == 'S02')) {//输入类型
    			newParamValue = $('#newInputVal').val().trim();
    			if (!newParamValue) {
    				alert("输入的值不能为空！");
    				return;
    			}
    		} else {//选择
    			newParamValue = $('#' + paramType + ' option:selected').val().trim();
    		}
    		if (!paramValue || paramValue == "") {
				paramValue = newParamValue;
			} else {
				if (_ifNewValueExists(paramValue, newParamValue)) {
	    			alert("您所输入的值已存在，请重新输入！");
	    			return;
	    		}
				paramValue = paramValue + "|" + newParamValue;
			}
			$(paramDiv).find("select[id='param_value']").attr("val", paramValue);
			var paramValueDesc = _getParamValueDesc(paramValue, paramType);
			_savaDefautValue(paramValue, paramValueDesc,paramType);
			doClose();
    	}
    	var doClose = function() {
    		$('#addParamValue').window('close');
    	}
    	_init();
    </script>
  </head>
  <style type="text/css">
		.p_c_actionParam {
			background-color:#EDF2F5;
			font-family:'新宋体';
			font-size:12px;
			font-weight:noml;
			padding:20px 5px;
			height:100%;
			overflow:hidden;
		}
	</style>
  <body>
    <div id="paramValueContent" class="p_c_actionParam">
    	<div id="paramValSelect">
    		请选择参数值:
    		<select id="S03">
    			<option value="2">预付</option>
    			<option value="1">后付</option>
    		</select>
    		<select id="S01">
    			<option value="Y">新装</option>
    			<option value="N">已有</option>
    			<option value="D">不需要</option>
    		</select>
    	</div>
    	<div id="paramValInput">请输入参数值:<input id="newInputVal" type="text"/></div>
	    <div class="btn_query">
		   <input type="button" class="btn-blue" onclick="doSave();" value="确定" />
		   <input type="button" class="btn-gray" onclick="doClose();" value="取消"/>
		</div>
    </div>

  </body>
</html>
